package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.StorageEntity;
import com.sgb.kjwl.model.entity.business.StorageManEntity;
import com.swgk.core.util.ACAdminDialogUtils;
import com.sgb.kjwl.view.ui.activity.self.publish.AddStorageActivity;
import com.sgb.kjwl.view.ui.adapter.self.publish.StorageAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

public class StoragePopWindow extends BasePopWindow {

    private XRecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private StorageAdapter storageAdapter;
    private LinearLayout noDataLay;
    private List<StorageEntity> storageEntityList;
    private LinearLayout linearLayout;
    private EditText editText;
    private InputTextInterface inputTextInterface;
    private StorageManEntity storageManEntity;
    private TextView tvBottomline;


    public ILodMore iLodMore;

    public interface ILodMore {
        void onRefresh();

        void onLoadMore();
    }

    public void onLodMoreListen(ILodMore iLodMore) {
        this.iLodMore = iLodMore;
    }

    public StoragePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_storage;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.skill_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        editText = holder.getPop().findViewById(R.id.et_view);
        noDataLay = holder.getPop().findViewById(R.id.ll_empty);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        linearLayout = holder.getPop().findViewById(R.id.ll_add);
        tvBottomline = holder.getPop().findViewById(R.id.tv_bottomline);

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(storageManEntity, null, null);
                }
            }
        });
        resetView.setOnClickListener(v -> dismiss());
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.IS_DATA, "").contains("3_cangchuguanli")) {
                    AddStorageActivity.start(mContext);
                } else {
                    ACAdminDialogUtils.showACDialog(ACAdminDialogUtils.TYPE_NO_PERMISSION, mContext);
                }
            }
        });
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    inputTextInterface.input(editText.getText().toString().trim());
                }
                return false;
            }
        });
        storageAdapter = new StorageAdapter(mContext, null);
        storageAdapter.setItemSelect(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(storageAdapter);
        storageAdapter.setOnItemClick(new AdapterOnItemClick<StorageManEntity>() {
            @Override
            public void onItemClick(StorageManEntity entity, int position) {
                storageManEntity = entity;
            }
        });
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                iLodMore.onRefresh();
            }

            @Override
            public void onLoadMore() {
                iLodMore.onLoadMore();
            }
        });
    }

    public void showAddll(boolean show){
        tvBottomline.setVisibility(show?View.VISIBLE:View.GONE);
        linearLayout.setVisibility(show?View.VISIBLE:View.GONE);

    }

    public void setData(List<StorageManEntity> list, boolean isLodMore) {
//        this.storageEntityList = list;
        recyclerView.refreshComplete();
        if (isLodMore) {
            if (null != list && list.size() > 0) {
                recyclerView.loadMoreComplete();
                storageAdapter.addDatas(list);
            } else {
                recyclerView.setNoMore(true);
            }
            recyclerView.setVisibility(View.VISIBLE);
        } else {
            if (list.size() == 0) {
                recyclerView.setVisibility(View.GONE);
                noDataLay.setVisibility(View.VISIBLE);
                storageAdapter.setDatasMayNull(list);
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                noDataLay.setVisibility(View.GONE);
                storageAdapter.setDefItem(-1);
                storageAdapter.setDatas(list);
            }
            recyclerView.refreshComplete();
            recyclerView.setLoadingMoreEnabled(true);
        }
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }

}
